package ro.cityprojects.adminloc.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import ro.cityprojects.adminloc.dao.BasicRepository;
import ro.cityprojects.adminloc.entities.Street;
import ro.cityprojects.adminloc.exceptions.EntityNotFoundException;
import ro.cityprojects.adminloc.exceptions.ValidationException;
import ro.cityprojects.adminloc.metadata.EntityMetadata;

import java.util.Set;

/**
 * StreetServiceImpl class
 *
 * @author bogdan.solga
 *
 * Date: 02.09.2013, time: 08:54
 */
@Service
public class StreetServiceImpl extends BaseService implements StreetService {

    @Autowired
    private BasicRepository<Integer, Street> repository;

    @Override
    public String[] getDefaultFields() {
        return new String[]{"streetId", "currentName", "oldName"};
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void save(Street street) throws ValidationException {
        repository.save(street);
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public String get(Integer primaryKey, String includedFields) throws EntityNotFoundException {
        Street street = repository.get(Street.class, primaryKey);

        String[] outputFields = getOutputFields(includedFields, getDefaultFields());

        return serialize(street, outputFields);
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public String getAll(String filterName, String filterValue, String includedFields) {
        Set<Street> all = repository.getAll(Street.class, filterName, filterValue);
        Assert.notEmpty(all, "There are no Streets with the specified details");

        String[] outputFields = getOutputFields(includedFields, getDefaultFields());

        return serialize(all, outputFields);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void update(Street street, Integer primaryKey) throws ValidationException {
        Street updated = repository.get(Street.class, primaryKey);

        BeanUtils.copyProperties(street, updated);

        repository.update(updated);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void delete(Integer primaryKey) throws EntityNotFoundException {
        repository.delete(Street.class, primaryKey);
    }

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
    public void checkExisting(Street street) throws ValidationException {
        String currentName = street.getCurrentName();
        Street existing = repository.getByProperty(Street.class, "currentName", currentName);
        if (existing != null) {
            throw new ValidationException("Exista deja o strada cu numele '" + currentName + "'");
        }
    }

    @Override
    public String getMetadata() {
        return getMetadata(EntityMetadata.Street);
    }
}
